<?php
/**
 * 链接管理 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_link.php 523 2010-06-23 10:06:24Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

check_perm_prompt('link');

empty($ac) && $ac = 'list';

if ($ac == 'list') {
	
	initGP(array('page','keyword','cid'));
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = 20;
	$offset = ($page - 1) * $pagesize;
	$url = "admin.php?mod=link&keyword=".rawurlencode($keyword)."&cid={$cid}";

	if ( $keyword ) $wheresql .= " AND (l.`name` LIKE '%$keyword%' OR l.url LIKE '%$keyword%')";
	if ( $cid ) $wheresql .= " AND l.cateid = $cid";

	$count = $db->count("link l", "1 $wheresql");

	$sql = "SELECT l.*,c.catename FROM ".tname('link')." l LEFT JOIN ".tname('cate')." c ON l.cateid = c.id 
	WHERE 1 $wheresql ORDER BY l.cateid ASC,l.orderid ASC LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);

} elseif ($ac == 'add') {
	
	check_perm_prompt('link','add');

	if ( !$db->count('cate', "`type`='link'") ) {
		prompt(__('当前还没有链接分类，请先添加一个分类。'), '?mod=link&ac=cate');
	}
	$result = array('status'=>1);

} elseif ($ac == 'edit') {
	
	check_perm_prompt('link','edit');
	
	if ( ! $result = $db->fetch_one_array("SELECT * FROM ".tname('link')." WHERE id = '".intval($_GET['id'])."'")) {
		prompt(__('链接不存在。'));
	}

} elseif ($ac == 'save') {
	
	initGP(array('id','cateid','name','url','logo','description','status'));
	if ( empty($name) ) prompt(__('网站名称不能为空。'));
	if ( empty($url) ) prompt(__('链接地址不能为空。'));
	$link = array(
		'cateid' => intval($cateid),
		'name' => check_str($name),
		'url' => check_str($url),
		'logo' => check_str($logo),
		'description' => check_str($description),
		'status' => intval($status)
	);
	if ( $id && is_numeric($id) ) {
		check_perm_prompt('link','eidt');
		$oldcid = $db->result("SELECT cateid FROM ".tname('link')." WHERE id = '$id'");
		$db->update('link', $link, array('id'=>$id));
		if ( $oldcid != $link['cateid'] ) {
			$count = $db->count('link', "cateid={$oldcid}");
			$db->update('cate', array('count'=>$count), array('id'=>$oldcid));
			$count = $db->count('link', "cateid={$link[cateid]}");
			$db->update('cate', array('count'=>$count), array('id'=>$link['cateid']));
		}
	} else {
		check_perm_prompt('link','add');
		$db->insert('link', $link);
		$db->query("UPDATE ".tname('cate')." SET `count` = `count` + 1 WHERE id = '$cateid'");
	}
	recache('link');
	do_action('link_save');
	redirect('?mod=link');

} elseif ( $ac == 'upstat' ) {
	
	check_perm_prompt('link','check');
	
	$val = $_GET['val']?1:0;
	$db->update('link', array('status'=>$val), array('id'=>intval($_GET['id'])));
	recache('link');
	do_action('link_upstat');
	redirect($_USER['refer']);

} elseif ($ac == 'del') {
	
	check_perm_prompt('link','del');
	
	$idarr = $upcate = array();
	foreach ( (array)$_POST['id'] as $id ) {
		if ( $id && is_numeric($id) ) {
			$idarr[] = $id;
		}
	}
	if ( $idarr ) {
		$query = $db->query("SELECT cateid FROM ".tname('link')." WHERE id IN (".simplode($idarr).")");
		while ( $row = $db->fetch_array($query) ) {
			$upcate[$row['cateid']]++;
		}
		$db->delete('link', array('id'=>$idarr));
		foreach ( $upcate as $key => $val ) {
			$db->query("UPDATE ".tname('cate')." SET `count` = `count` - '$val' WHERE id = '$key'");
		}
		recache('link,cate');
		do_action('link_del');
	}
	redirect($_USER['refer']);

} elseif ($ac == 'move') {
	
	check_perm_prompt('link','edit');

	$idarr = $upcate = array();
	foreach ( (array)$_POST['id'] as $id ) {
		if ( $id && is_numeric($id) ) {
			$idarr[] = $id;
		}
	}
	$cid = intval($_POST['cateid']);
	if ( $db->result("SELECT id FROM ".tname('cate')." WHERE id = '$cid' AND `type` = 'link'") ) {
		$upcate[] = $cid;
		$query = $db->query("SELECT cateid FROM ".tname('link')." WHERE id IN (".simplode($idarr).")");
		while ( $row = $db->fetch_array($query) ) {
			$upcate[]=$row['cateid'];
		}
		$db->update('link', array('cateid'=>$cid), array('id'=>$idarr));
		foreach ( $upcate as $val ) {
			$count = $db->count('link', "cateid = '$val'");
			$db->update('cate', array('count'=>$count),array('id'=>$val));
		}
		recache('link,cate');
		do_action('link_move');
	}
	redirect($_USER['refer']);

} elseif ($ac == 'resort') {
	
	check_perm_prompt('link','edit');
	
	foreach ( $_POST['order'] as $key => $val ) {
		$db->update('link', array('orderid'=>intval($val)), array('id'=>intval($key)) );
	}
	recache('link');
	do_action('link_resort');
	redirect($_USER['refer']);
	

} elseif ( $ac == 'cate' ) {
	
	check_perm_prompt('link','cate');
	
	$sql = "SELECT * FROM ".tname('cate')." WHERE `type` = 'link' ORDER BY orderid";
	$result = $db->fetch_all($sql);

} elseif ( $ac == 'savecate' ) {
	
	check_perm_prompt('link','cate');
	
	foreach ( (array)$_POST['catename'] as $key => $val ) {
		if ( !empty($val) ) {
			$val = check_str($val);
			$orderid = intval($_POST['orderid'][$key]);
			$count = $db->count('link', "cateid={$key}");
			$db->update('cate',array('catename'=>$val,'orderid'=>$orderid,'count'=>$count), array('id'=>intval($key)));
		}
	}
	if ( $_POST['newname'] ) {
		$cate = array('catename'=>check_str($_POST['newname']), 
		'orderid'=>intval($_POST['neworderid']), 'type'=>'link');
		$db->insert('cate', $cate);
	}
	recache('cate');
	do_action('link_savecate');
	redirect('?mod=link&ac=cate');
	
} elseif ( $ac == 'delcate' ) {
	
	check_perm_prompt('link','cate');
	
	$id = intval($_GET['id']);
	$link = $db->fetch_one_array("SELECT * FROM ".tname('cate')." WHERE id = '$id' AND `type` = 'link'");
	if ( $link ) {
		if ( $link['count'] > 0 ) {
			prompt(__('请将该分类下的链接移动到其他分类再删除。'));
		} else {
			$db->delete('cate', array('id'=>$id));
		}
	}
	do_action('link_delcate');
	redirect('?mod=link&ac=cate');
	
}

include admin_tpl('link');
?>